package com.ls.lserp.modules.material.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.CollectionsUtils;
import com.ls.lserp.common.utils.ResourcesUtil;
import com.ls.lserp.modules.material.dao.SlopworkOutboundDao;
import com.ls.lserp.modules.material.entity.MaterialOutInbound;
import com.ls.lserp.modules.material.entity.SlopworkInbound;
import com.ls.lserp.modules.material.entity.SlopworkOutbound;

@Service
@Transactional(readOnly = false)
public class SlopworkOutboundService extends CrudService<SlopworkOutboundDao,SlopworkOutbound>{
	
	 @Autowired
	 private SlopworkOutboundDao slopworkOutboundDao;
	 
	 /**
		 * @Title: findList
		 * @Description: 一览查询
		 * @param page
		 * @param orderMateriel
		 * @return Page<OrderMateriel>
		 */
		public Page<SlopworkOutbound> findList(Page<SlopworkOutbound> page, SlopworkOutbound slopworkOutbound) {
			List<SlopworkOutbound> irDetailList = null;
			BigDecimal tempRealQuantity = null;
			
			// tab页分，检索条件设定(0:全部,1:待入库,2:未全部入库,3:全部入库)
			slopworkOutbound.setOutboundStatus(ResourcesUtil.getConfig("outbound_status_wait"));
			
			// 入出库区分（入库)
			slopworkOutbound.setOutboundInKbn(ResourcesUtil.getConfig("inventory_in_out_kbn_out"));
			
			// 设置分页参数
			slopworkOutbound.setPage(page);
			List<SlopworkOutbound> resultList = slopworkOutboundDao.findList(slopworkOutbound);
			
			if(!CollectionsUtils.isEmpty(resultList)){
				// 库存申请信息
				for(SlopworkOutbound ir : resultList){
					tempRealQuantity = new BigDecimal("0");
					irDetailList = slopworkOutboundDao.findRealQuantityList(ir);
					if(!CollectionsUtils.isEmpty(irDetailList)){
						// 库存申请明细
						for(SlopworkOutbound ird : irDetailList){
							tempRealQuantity = tempRealQuantity.add(checkBigDecimalNull(ird.getRealQuantity()));
						}
						
						// 实际出库数量
						ir.setRealQuantity(tempRealQuantity);
						
						// 金额(单价*实际出库数量)
						ir.setTotalAmount(tempRealQuantity.multiply(checkBigDecimalNull(ir.getIndentPrice())).setScale(2, BigDecimal.ROUND_HALF_UP));
					}
				}
			}
			
	        // 执行分页查询
	        page.setList(resultList);
	        
	        return page;
		}
		
		/**
		 * @Title: getClothesRequest
		 * @Description: 出库申请明细
		 * @param @param slopworkOutbound
		 * @param @return
		 * @return SlopworkOutbound
		 * @throws
		 */
		public SlopworkOutbound getClothesRequest(SlopworkOutbound slopworkOutbound) {
		   
		   return slopworkOutboundDao.getClothesRequest(slopworkOutbound);
		}

		/**
		 * @Title: findShowRequestDetailList
		 * @Description: 出库申请明细
		 * @param @param slopworkOutbound
		 * @param @return
		 * @return List<SlopworkOutbound>
		 * @throws
		 */
		public List<SlopworkOutbound> findShowRequestDetailList(SlopworkOutbound slopworkOutbound) {
			List<SlopworkOutbound>  dataList = new ArrayList<>();
           if(ResourcesUtil.getConfig ("clothes_out_category_Samply").equals(slopworkOutbound.getOutboundCategory())){
			   dataList =  slopworkOutboundDao.findShowSamplyBoundDetailList(slopworkOutbound);
		   }else{
			   dataList =slopworkOutboundDao.findShowRequestDetailList(slopworkOutbound);
		   }
		   return dataList;
		}

		/**
		 * @Title: findClothesInventoryDetails
		 * @Description: 成衣库存明细
		 * @param @param slopworkOutbound
		 * @param @return
		 * @return List<SlopworkOutbound>
		 * @throws
		 */
		public List<SlopworkOutbound> findClothesInventoryDetails(SlopworkOutbound slopworkOutbound) {				
			   slopworkOutbound.setOutboundInKbn(ResourcesUtil.getConfig("inventory_in_out_kbn_in"));
			
			   List<SlopworkOutbound> rtnList = slopworkOutboundDao.findClothesInventoryDetails(slopworkOutbound);
			   for(SlopworkOutbound sob : rtnList){
				   sob.setClothsQuantity(checkBigDecimalNull(sob.getInventoryQuantity()).subtract(checkBigDecimalNull(sob.getRealQuantity())));
			   }
			   return rtnList;
		}
				
		/**
		 * check BigDecimal
		 * @param cost
		 * @return 取不到返回0
		 */
		public BigDecimal checkBigDecimalNull(BigDecimal cost){
			if(cost==null){
				return new BigDecimal("0");
			}
			return cost;
		}
		
		/**
		    * @Title: saveInventoryRequest
		    * @Description: 保存出库申请
		    * @param @param inventoryRequest
		    * @return void 
		    * @throws
		    */
		   @Transactional(readOnly = false)
		   public void saveSlopworkDetail(SlopworkOutbound slopworkOutbound) {
		   	// 判断list是否为空
		   	if(!CollectionsUtils.isEmpty(slopworkOutbound.getClothsOutDetailsList())){
		   		slopworkOutbound.setOutboundInKbn(ResourcesUtil.getConfig("inventory_in_out_kbn_out"));
		   	   // 遍历赋值
		          for(SlopworkOutbound itr : slopworkOutbound.getClothsOutDetailsList()){
		         	 itr.preInsert();
		         	 itr.setVersion(1L);
		         	 itr.setOutRequestDetailId(slopworkOutbound.getOutRequestDetailId());
		         	 itr.setDeleteFlag(MaterialOutInbound.DEL_FLAG_NORMAL);
		         	 itr.setEnableFlag(MaterialOutInbound.ENABlE_FLAG_NORMAL);
					  //已出库
		         	 itr.setMaterialStatus(ResourcesUtil.getConfig("outbound_status_all"));
		         	 itr.setOutboundInKbn(ResourcesUtil.getConfig("inventory_in_out_kbn_out"));
		         	 itr.setOutboundCategory(slopworkOutbound.getOutboundCategory());
					  if("".equals(itr.getClothsQuantity())){
						  //删除数据
						  slopworkOutboundDao.deleteMaterialDetail(itr);
						  slopworkOutboundDao.insertDetail(itr);
					  }
		   		}
		   	}
		   }

		   /**
			   * @Title: saveDetail
			   * @Description: 保存实际数量
			   * @param @param materialOutInbound
			   * @return void 
			   * @throws
			   */
			  @Transactional(readOnly = false)
			  public void saveQuantity(SlopworkOutbound slopworkOutbound) {
				  slopworkOutbound.preInsert();
				  slopworkOutboundDao.updateDetail(slopworkOutbound);
				  //重新获取实际库存的总数
				  SlopworkOutbound sloOutbound = slopworkOutboundDao.getQuantity(slopworkOutbound);
				  if( slopworkOutbound.getRealOutQuantity() != null && sloOutbound.getClothsQuantity()!=null){

					  slopworkOutbound.setClothsQuantity(sloOutbound.getClothsQuantity().subtract(slopworkOutbound.getRealOutQuantity()));
				  }
				  slopworkOutboundDao.updateQuantity(slopworkOutbound);
			  }

		/**
		   * @Title: saveDetail
		   * @Description: 更新状态
		   * @param @param materialOutInbound
		   * @return void 
		   * @throws
		   */
		  @Transactional(readOnly = false)
		  public void changeStatus(SlopworkOutbound slopworkOutbound) {
			  slopworkOutbound.preInsert();
			  slopworkOutboundDao.changeStatus(slopworkOutbound);
		  }
		

		
		
		
}
